package com.oa.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/BaseServlet")
public class BaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {		
		//解码
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		
		//1. 获取要调用的方法名,方法名来自前端的键值对
		String methodName=req.getParameter("method");	
//		System.out.println("请求的方法名==>"+methodName);
		//2. 通过反射获取当前请求的类
		Class clazz=this.getClass();
		try {
			//2. 通过反射获取方法，getMethod(方法名，参数类型列表...)
			Method method=clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			//3. 通过反射调用方法,invoke(对象,实参列表...)
			method.invoke(this, req,resp);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
